package com.faceye.core.util.base64;

import java.io.File;
import java.io.IOException;
import java.util.Base64;

import org.apache.commons.io.FileUtils;
import org.aspectj.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Base64工具类
 * @author haipenge
 *
 */
public class Base64Utils {
	private static Logger logger=LoggerFactory.getLogger(Base64Utils.class);
	public static  File base642File(String base64,String targetPath){
		File file=new File(targetPath);
		byte [] buffer=Base64.getDecoder().decode(base64);
		try {
			FileUtils.writeByteArrayToFile(file, buffer);
		} catch (IOException e1) {
			logger.error(">>FaceYe Throws Exception:",e1);
		}
		return file;
	}
	/**
	 * 文件转Base64
	 * @param path
	 * @return
	 * @Desc:
	 * @Author:haipenge
	 * @Date:2018年12月17日 下午2:15:15
	 */
	public static String file2Base64(String path){
		String base64="";
		try {
			byte [] buffer = FileUtil.readAsByteArray(new File(path));
			base64=Base64.getEncoder().encodeToString(buffer);
		} catch (IOException e) {
			logger.error(">>FaceYe Throws Exception:",e);
		}
		return base64;
	}
}
